package com.sy.structural.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibProxy {


    public static<T> T createProxy(Class<T> target) {
        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(target);

        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o,
                                    Method method,
                                    Object[] args,
                                    MethodProxy methodProxy) throws Throwable {

                // 调用父类的方法
                Object invoke = methodProxy.invokeSuper(o, args);
                // 增强
                System.out.println("cglib增强了");

                return invoke;
            }
        });

        Object o = enhancer.create();
        return (T)o;

    }

}
